package com.gohome.gohomechat.utils;

import android.app.ActivityManager;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.easemob.chat.EMMessage;
import com.easemob.chat.TextMessageBody;
import com.easemob.util.EMLog;
import com.easemob.util.HanziToPinyin;
import com.gohome.gohomechat.model.entify.Contant;

import java.util.List;

/**
 * Created by dllo on 16/3/8.
 */
public class CommonUtils {
    private static final String TAG = "CommonUtils";

    /**
     * 检查网络是否可用
     * @param context
     * @return
     */
    public static boolean isNetWorkConnected(Context context){
        if (context != null){
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo != null){
                return mNetworkInfo.isAvailable() && mNetworkInfo.isConnected();
            }
        }
        return false;
    }

    /**
     * 检查SD卡是否存在
     * @return
     */
    public static boolean isExitsSdcard(){
        if (android.os.Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
            return true;
        else
            return true;
    }

    public static EMMessage createExpressionMessage(String toChatUsername,String expressioName,String identityCode){
        EMMessage message = EMMessage.createTxtSendMessage("[" + expressioName + "]",toChatUsername);
        if (identityCode != null){
            message.setAttribute(Contant.MESSAGE_ATTR_EXPRESSION_ID,identityCode);
        }
        message.setAttribute(Contant.MESSAGE_ATTR_IS_BIG_EXPRESSION,true);
        return message;
    }

    public static String getMessageDigest(EMMessage message,Context context){
        String digest = "";
        switch (message.getType()){
            case LOCATION: //位置消息
                if (message.direct == EMMessage.Direct.RECEIVE){
                    //"从sdk中提到ui中,使用更简单不犯错的获取string方法"
                      digest = "[{R.string.location_recv} location}";
                    digest = String.format(digest,message.getFrom());
                    return digest;
                }else {
                    digest = "[my location]";
                }
                break;
            case IMAGE://图片消息
                digest = "[Picture]";
                break;
            case VOICE: //语音消息
                digest = "[Voice]";
                break;
            case VIDEO: //视频消息
                digest ="[Video]";
                break;
            case TXT: //文本消息
                TextMessageBody txtBody = (TextMessageBody) message.getBody();
                if (message.getBooleanAttribute(Contant.MESSAGE_ATTR_IS_VOICE_CALL,false)){
                    digest = "[Voice call]" + txtBody.getMessage();
                }else if (message.getBooleanAttribute(Contant.MESSAGE_ATTR_IS_BIG_EXPRESSION,false)){
                    if (!TextUtils.isEmpty(txtBody.getMessage())){
                        digest = txtBody.getMessage();
                    }else {
                        digest = "[Dynamic expression]";
                    }
                }else {
                    digest = txtBody.getMessage();
                }
                break;
            case FILE: //普通文件消息
                digest = "[file]";
                break;
            default:
                EMLog.e(TAG,"ERROR,unknown type");
                return "";
        }
        return digest;
    }
    static String getString(Context context,int resId){
        return context.getResources().getString(resId);
    }

    /**
     * 获取栈顶的activity
     * @param context
     * @return
     */
    public static String getTopActivity(Context context) {
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);

        if (runningTaskInfos != null)
            return runningTaskInfos.get(0).topActivity.getClassName();
        else
            return "";
    }

    public static void showSoftInput(Context context, View view){
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
        //imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
    }

    public static void hideSoftInput(Context context, View view){
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0); //强制隐藏键盘
    }

    public static boolean isShowSoftInput(Context context){
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        //获取状态信息
        return imm.isActive();//true 打开
    }
}
